package com.zking.dao;

import com.zking.entity.Address;
import com.zking.entity.Order;
import com.zking.entity.User;

import java.sql.SQLException;
import java.util.List;

public class OrderDao extends BaseDaoImpl{
    /**
     * 查询本用户所有未删除的订单
     *
     * @return
     */
    public List<Order> queryOrder(String username) {
        String sql = "select * from `order` WHERE username= '" + username+"'and dstate = 'F';";
        try {
            return findList(Order.class, sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<Order> queryOrderF(String username,String state){
        String sql = "select * from Order WHERE username= '" + username + "' and state = '" + state + "';";

        try {
            return  findList(Order.class,sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    /*
        添加订单
     */
    public int InsertOrder(Order order){
        //  String sql ="insert into user values(?,?,?,?,?,?)";
        String sql ="INSERT INTO `rz1217db_2`.`order`(`Username`, `Shopname`, `Time`,`Pirce`,`State`) VALUES (?,?,?,?,?)";
        Object[] params = {order.getUsername(),order.getShopname(),order.getTime(),order.getPirce(),order.getState()};
        try {
            return runner.update(sql,params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
    public int DeleteOrder(Order order) {
        String sql ="UPDATE `order` SET `dstate` = 'T' WHERE `username` = ? AND `shopname` = ? AND `time` = ? AND `pirce` = ? AND `state` = ?";
        // UPDATE `order` SET `dstate` = 'T' WHERE `username` = ? AND `shopname` = ? AND `time` = ? AND `pirce` = ? AND `state` = ?
        Object[] params = {order.getUsername(),order.getShopname(),order.getTime(),order.getPirce(),order.getState()};
        try {
            return update(sql, params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 按状态查询
     * @param username
     * @param state
     * @return
     */
     public List<Order> queryOrder1(String username,String state) {
        String sql = "select * from `order` WHERE username= '" + username+"' AND state='" + state+"';";
        try {
            return findList(Order.class, sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    /*
    修改状态
     */
    public int updateOrder(String state,Order order1) {
        String sql = "UPDATE `rz1217db_2`.`order` SET `state` = ? WHERE `username` = ? AND `shopname` = ? AND `time` = ? AND `pirce` = ? AND `state` = ? ";
        // UPDATE `rz1217db_2`.`address` SET `name` = '静安大火的话', `address` = '啊回答后', `phone` = '15675276365' WHERE `name` = '阿斯弗' AND `address` = '公司给对方卡接口' AND `phone` = '18508402409' AND `username` = '654321'
        Object[] params = {state,order1.getUsername(),order1.getShopname(),order1.getTime(),order1.getPirce(),order1.getState()};
        try {
            return update(sql, params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
